package com.zjdiepu.www.manage.view;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.enums.type.DataDicType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.commpent.ViewScopeControllerHelper;
import com.zjdiepu.www.ibiz.base.IDpBaseDataDictionaryService;
import com.zjdiepu.www.ibiz.cache.ICachedDictionaryService;
import com.zjdiepu.www.ibiz.cw.ICwIncomeExpensesService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessExtrasService;
import com.zjdiepu.www.ibiz.yw.IYwFeesSubService;
import com.zjdiepu.www.model.base.dto.DpBaseDataDictionary;
import com.zjdiepu.www.model.base.ext.DpBaseDataDictionaryExt;
import com.zjdiepu.www.model.cw.dto.CwIncomeExpenses;
import com.zjdiepu.www.model.cw.ext.CwIncomeExpensesExt;
import com.zjdiepu.www.model.cw.response.CwFundItem;
import com.zjdiepu.www.model.yw.response.YwBusinessDetail;
import com.zjdiepu.www.util.ExceptionHelper;

/**
 * 收支管理Controller层<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年05月11日 20时36分<br/>
 * @since 1.0<br/>
 */
@Controller
@RequestMapping(value= "CwincomeExpensesViewController", produces="text/html;charset=UTF-8")
@Api(tags = {"收支管理"}, produces="text/html;charset=UTF-8")
public class CwIncomeExpensesViewController extends BaseController {
    @Resource
    private ICwIncomeExpensesService cwincomeExpensesService;
    @Resource
    private ViewScopeControllerHelper viewScopeControllerHelper;
    @Resource
    private IYwBusinessExtrasService ywBusinessExtrasService;
    @Resource
    private IYwFeesSubService ywFeesSubService;
    @Resource
    private ICachedDictionaryService cachedDictionaryService;
    @Resource
    private IDpBaseDataDictionaryService dBaseDataDictionaryService;
    
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: incomeExpensesDetail <br/>
     * @Description: 收支管理详情 <br/>
     * @param model
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "收支管理详情", response = Boolean.class, httpMethod="GET", tags={"收支管理"})
    @RequestMapping(value = "incomeExpensesDetail", method = RequestMethod.GET)
    public String incomeExpensesDetail(Model model, String id) {
    	if (StringUtils.isBlank(id)) {
			ExceptionHelper.thow(ResultFlagType.PARAM_EMPTY);
		}
    	model.addAttribute("itemdirection", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.FUND_DIRECTION,"请选择..."));
    	model.addAttribute("settlementTypes", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.SETTLEMENT_TYPE,"请选择..."));
    	model.addAttribute("fundAccount", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.FUND_ACCOUNT, "请选择..."));
    	
    	
    	List<DpBaseDataDictionary> dpBaseDataDictionarys = CollectionInitUtil.emptyList();
    	DpBaseDataDictionaryExt depositItemExt = new DpBaseDataDictionaryExt();
    	depositItemExt.setBaseWhere();
    	depositItemExt.setRows(1);
    	depositItemExt.setParentValue(DataDicType.FUND_ITEM.getCode());
    	depositItemExt.setValue("01");
    	List<DpBaseDataDictionary> depositDataDictionaryItems_ = dBaseDataDictionaryService.selectByExt(depositItemExt);
    	if(CollectionUtils.isNotEmpty(depositDataDictionaryItems_)) {
    		dpBaseDataDictionarys.add(depositDataDictionaryItems_.get(0));
    	} else {
    		DpBaseDataDictionary dpBaseDataDictionary = new DpBaseDataDictionary();
    		dpBaseDataDictionary.setValue("01");
    		dpBaseDataDictionary.setName("按揭服务费");
    		dpBaseDataDictionary.setParentValue(DataDicType.FUND_ITEM.getCode());
    		dpBaseDataDictionarys.add(dpBaseDataDictionary);
    	}
    	DpBaseDataDictionaryExt dpBaseDataDictionaryExt = new DpBaseDataDictionaryExt();
    	dpBaseDataDictionaryExt.setBaseWhere();
    	dpBaseDataDictionaryExt.withOutPage();
    	dpBaseDataDictionaryExt.setParentValue(DataDicType.FUND_ITEM.getCode());
    	dpBaseDataDictionaryExt.setAddedValue1("01"); //启用的明细
    	dpBaseDataDictionaryExt.setOrderSql("`VALUE`");
    	dpBaseDataDictionarys.addAll(dBaseDataDictionaryService.selectByExt(dpBaseDataDictionaryExt));
    	
    	depositItemExt.setBaseWhere();
    	depositItemExt.setRows(1);
    	depositItemExt.setParentValue(DataDicType.FUND_ITEM.getCode());
    	depositItemExt.setValue("35");
    	List<DpBaseDataDictionary> depositDataDictionaryItems = dBaseDataDictionaryService.selectByExt(depositItemExt);
    	if(CollectionUtils.isNotEmpty(depositDataDictionaryItems)) {
    		dpBaseDataDictionarys.add(depositDataDictionaryItems.get(0));
    	} else {
    		DpBaseDataDictionary dpBaseDataDictionary = new DpBaseDataDictionary();
    		dpBaseDataDictionary.setValue("35");
    		dpBaseDataDictionary.setName("征信押金");
    		dpBaseDataDictionary.setParentValue(DataDicType.FUND_ITEM.getCode());
    		dpBaseDataDictionarys.add(dpBaseDataDictionary);
    	}
    	
    	
    	Map<String, String> fundItems = new LinkedHashMap<String, String>();
    	if(CollectionUtils.isNotEmpty(dpBaseDataDictionarys)) {
    		for(DpBaseDataDictionary dpBaseDataDictionary : dpBaseDataDictionarys) {
    			fundItems.put(dpBaseDataDictionary.getValue(), dpBaseDataDictionary.getName());
    		}
    	}
    	model.addAttribute("fundItems", fundItems);
    	
    	List<CwFundItem> feeFundItems = CollectionInitUtil.emptyList();
    	List<Map<String, Double>> fees = ywFeesSubService.countReceiveByBusiId(id, null);
    	if(CollectionUtils.isNotEmpty(dpBaseDataDictionarys)) {
    		for(DpBaseDataDictionary dpBaseDataDictionary : dpBaseDataDictionarys) {
    			CwFundItem cwFundItem = new CwFundItem();
    			String key = dpBaseDataDictionary.getValue();
    			cwFundItem.setKey(key);
    			cwFundItem.setName(dpBaseDataDictionary.getName());
    			cwFundItem.setFeeCount(ViewScopeControllerHelper.getItem(fees, key));
    			feeFundItems.add(cwFundItem);
    		}
    	}
    	model.addAttribute("feeFundItems", feeFundItems);
    	
    	viewScopeControllerHelper.preparedNowTime(model);
    	YwBusinessDetail ywBusinessDetail = viewScopeControllerHelper.preparedBusiness(id, model);
    	if(ywBusinessDetail != null){
    		CwIncomeExpensesExt cwIncomeExpensesExt = new CwIncomeExpensesExt();
    		cwIncomeExpensesExt.setBaseWhere();
    		cwIncomeExpensesExt.setRows(1);
    		cwIncomeExpensesExt.setBusiId(ywBusinessDetail.getId());
    		List<CwIncomeExpenses> cwIncomeExpenses = cwincomeExpensesService.selectByExt(cwIncomeExpensesExt);
    		if(CollectionUtils.isNotEmpty(cwIncomeExpenses)) {
    			model.addAttribute("cwIncomeExpenses", cwIncomeExpenses.get(0));
    		}
    	}
    	
    	return "system/cw/incomeExpensesDetail";
    }
	
}
